from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseRedirect, Http404
from skoolmaster.general.common import InsufficientAuthorisation 

class AuthorisationMiddleware(object):
  """
  Middleware responsible for handling skoolmaster authorisation-related
  exceptions.
  """
  def process_exception(self, request, exception):
    """
    Exception handler.
    """
    if isinstance(exception, InsufficientAuthorisation):
      # Display an error notification to the user
      if request.user.is_authenticated():
        request.user.message_set.create(message = u"2|Za prikaz navedene strani nimate zadostnih pravic!")
      
      return HttpResponseRedirect(reverse("welcome"))
    
    return None

